load('KeyResults.mat');

List_Prob_ext   =   (0.10:0.1:0.90)';
Num_Prob_ext    =   length(List_Prob_ext);

List_Fin_AdjCost=   [0.01,0.05,0.1,0.2,0.4,0.8,2,5,10,50,100,1000];
Num_Fin_AdjCost =   length(List_Fin_AdjCost);
XAxis       =   log(List_Fin_AdjCost)-log(0.8);
YAxis       =   List_Prob_ext;

ZAxis_AggC  =   zeros(size(KeyResults));
ZAxis_StdC  =   zeros(size(KeyResults));
for rr=1:size(KeyResults,1)
    for cc=1:size(KeyResults,2)
        ZAxis_AggC(rr,cc)    =   KeyResults{rr,cc}(1,2);
        ZAxis_StdC(rr,cc)    =   KeyResults{rr,cc}(5,2);
    end
end
%% Iso-AggC Curves
Levels  =   GR_TickValue('Unit',0.02).Setup(ZAxis_AggC);
M       =   contour(XAxis,YAxis,ZAxis_AggC,Levels);
close();
N_Levels=   length(Levels);
Lines   =   cell(N_Levels,1);
N0       =   1;
for ii=1:N_Levels
    Lines{ii}   =   struct('v',M(1,N0),'n',M(2,N0),...
                          'x',M(1,N0+(1:M(2,N0))),'y',M(2,N0+(1:M(2,N0))));
    N0           =   N0+M(2,N0)+1;
    if N0>size(M,2)
        break;
    end
end
N_Lines =   ii;
Lines   =   Lines(1:N_Lines);

% Figure 1
N_col       =   1;
N_row       =   1;
fig_size    =   [N_col,N_row].*[1,0.75]*1/2.5; 
fig_gap     =   [0.02,0.02];
fig_VMargin =   [0.15,0.02];
fig_HMargin =   [0.18,0.02]; 

fig_SubPlot =   @(ii_vv)subtightplot(N_row,N_col,ii_vv,...
                                     fig_gap,fig_VMargin,fig_HMargin);
LEG         =   @(PH,LabelList)legend(PH,LabelList,'fontsize',8,'interpreter','latex','location','best');
XLabel      =   @(Label)xlabel(Label,'fontsize',8,'interpreter','latex');
YLabel      =   @(Label)ylabel(Label,'fontsize',8,'interpreter','latex');

AxisSetup   =   GR_Axis('TickFmt',struct('X','%g','Y','%.2g'),...
                        'FontSize',struct('X',8,'Y',8),...
                        'Exponent',struct('X',0,'Y',0),...
                        'Box','off');

Tick_X      =   @(X)GR_TickValue('Unit',4).Setup(X);
Tick_Y      =   @(Y)GR_TickValue('Symmetric',0).Setup(Y);

TempFun_LP  =   @(C)GR_Line('Style','-','Color',MyColor('Blue',C),'Width',3);

Fig         =   FigureSetup(['Iso-AggC Curves'],fig_size);
ax          =   fig_SubPlot(1);
for ii=1:N_Lines
    TempFun_LP(0.1+0.89*ii/N_Lines).Plot(Lines{ii}.x,Lines{ii}.y);
    hold on;
end
AxisSetup.Setup(ax);
xlim([min(XAxis),max(XAxis)]);
ylim([min(YAxis),max(YAxis)]);
xticklabels(round(exp(xticks+log(0.8)),2));

%% Std
[YY,XX]     =   ndgrid(YAxis,XAxis);
F           =   griddedInterpolant(YY,XX,ZAxis_StdC);

Fig         =   FigureSetup(['C-Std Curves'],fig_size);
ax          =   fig_SubPlot(1);
for ii=1:N_Lines
    TempFun_LP(0.1+0.89*ii/N_Lines).Plot(Lines{ii}.x,F(Lines{ii}.y,Lines{ii}.x));
    hold on;
end
AxisSetup.Setup(ax);
xlim([min(XAxis),max(XAxis)]);
% ylim([min(YAxis),max(YAxis)]);
xticklabels(round(exp(xticks+log(0.8)),2));

%%
Fig         =   FigureSetup(['C-Std Curves'],fig_size);
ax          =   fig_SubPlot(1);
for ii=1:N_Lines
    TempFun_LP(0.1+0.89*ii/N_Lines).Plot(Lines{ii}.y,F(Lines{ii}.y,Lines{ii}.x));
    hold on;
end
AxisSetup.Setup(ax);
xlim([min(YAxis),max(YAxis)]);
% ylim([min(YAxis),max(YAxis)]);
% xticklabels(round(exp(xticks+log(0.8)),2));